<template>

</template>

<script>
    import {PubSubManager} from "../../QtUtils";
    import {
      COMPONENT_TYPE_MENU,
      COMPONENT_TYPE_VIEW,
      DIRECTION_HIDE_BOTTOM,
      DIRECTION_HIDE_LEFT,
      DIRECTION_HIDE_RIGHT,
      DIRECTION_HIDE_TOP,
      FrameManager
    } from "../FrameManager";

    export default {
      name: "BaseComponent",

      data() {
        return {
          unique_name :'base',     //唯一标识
          component_type:COMPONENT_TYPE_VIEW, //默认是view
          isSelfShow:false,    //自身是否显示
          componentzIndex:1,   //view的zIndex
          isMounted:false,
          isLeftHide:false,     //左侧区域是否隐藏
          isRightHide:false,     //右侧区域是否隐藏
          isTopHide:false,     //上侧区域是否隐藏
          isBottomHide:false,     //下侧区域是否隐藏
        };
      },
      mounted() {
        if(this.component_type == COMPONENT_TYPE_MENU) {
          //菜单类型的则做唯一校验
          if(this.unique_name == 'base' || this.unique_name == undefined || this.unique_name == null) {
            let msg = 'unique_name无效--' + this.unique_name;
            alert(msg);
            throw(msg);
          }
          // if(FrameManager.getInstance().isContainerComponentName(this.unique_name)) {
          //   let msg = 'unique_name重复--' + this.unique_name;
          //   alert(msg);
          //   throw(msg);
          // }
          FrameManager.getInstance().addMenu(this.unique_name);
        }
        FrameManager.getInstance().addComponentName(this.unique_name);
        this.isMounted = true;
        this.onMounted();
      },
      created() {
        this.onCreated();
        //接收消息
        PubSubManager.addEvent(this,'PubSubMessage',(msg,data)=>{
          let strCode = data['strCode'];
          let excludeComponent = data['excludeComponent'];
          let realData = data['data'];
          if(excludeComponent.length == 0 || !excludeComponent.includes(this.unique_name)) {
            this.onPubSubMessage(strCode,realData);
          }
        })

        //监听切换Component的消息
        PubSubManager.addEvent(this,'switchComponent',(msg,data)=>{
          let curName = data['curName'];
          let lastName = data['lastName'];
          this.onSwitchComponent(curName,lastName);
        })

        //页面显隐切换
        PubSubManager.addEvent(this,'preViewShow',(msg,data)=>{
          let strName = data[0];
          let isShow = data[1];
          if(strName == this.unique_name) {
            let viewParam = FrameManager.getInstance().GetViewParam(strName);
            let isChange = (isShow != viewParam.isPreShow);
            if(isShow && isChange) {
              this.onPreEnter();
            }else if(!isShow && isChange){
              this.onPreExit();
            }
          }
        })

        //执行visible事件
        PubSubManager.addEvent(this,'setViewShow',(msg,data)=>{
          let strName = data[0];
          let isShow = data[1];
          let zIndex = data[2];
          if(strName == this.unique_name) {
            this.isSelfShow = isShow;
            this.component_zIndex = zIndex;
          }
        })

        //执行visible事件
        PubSubManager.addEvent(this,'postViewShow',(msg,data)=>{
          let strName = data[0];
          let isShow = data[1];
          let zIndex = data[2];
          if(strName == this.unique_name) {
            let viewParam = FrameManager.getInstance().GetViewParam(strName);
            let isChange = (isShow != viewParam.isPreShow);
            if(isShow && isChange) {
              this.onEnter();
            }else if(!isShow && isChange){
              this.onExit();
            }
          }
          this.onViewVisible(strName,isShow,zIndex);
        })

        //执行visible事件
        PubSubManager.addEvent(this,'setViewComponentDirectHide',(msg,data)=>{
          this.isLeftHide = false;
          this.isRightHide = false;
          this.isTopHide = false;
          this.isBottomHide = false;

          let nDirectCode = data[0];
          if(nDirectCode&DIRECTION_HIDE_LEFT) {
            this.isLeftHide = true;
          }
          if(nDirectCode&DIRECTION_HIDE_RIGHT) {
            this.isRightHide = true;
          }
          if(nDirectCode&DIRECTION_HIDE_TOP) {
            this.isTopHide = true;
          }
          if(nDirectCode&DIRECTION_HIDE_BOTTOM) {
            this.isBottomHide = true;
          }
          this.onDirectHideState(nDirectCode);
        })

        //restartEnter
        PubSubManager.addEvent(this,'restartEnter',(msg,data)=>{
          if(data.included(this.unique_name)) {
            this.onEnter();
          }
        })
      },
      destroyed() {
        this.onExit();
        this.onDestroyed();
        PubSubManager.removeEvent(this);
      },
      methods:{
        onMounted() {

        },
        onCreated() {},
        onDestroyed() {},
        /**
         * PubSub接收消息
         */
        onPubSubMessage(strCode,data) {},
        onViewVisible(strName,isShow,zIndex) {},    //页面显隐切换
        onPreEnter() {},    //还未进入（未显示前）
        onEnter() {},
        onPreExit() {},     //还未进入（未隐藏前）
        onExit() {},
        onSwitchComponent(curName,lastName) {},
        onDirectHideState(nDirectCode) {},    //页面上下左右区域的隐藏状态变化
      }
    }
</script>

<style scoped>

</style>
